package com.jrx.anytxn.param.transaction.job.paph.step;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.param.entity.TlFilePaymentLog;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.entity.TlPaymentAllocationLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.mapper.ext.*;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: anytxn-cf-parent
 * @description:
 * @author: xiaoyu.jing
 * @date: 2021-04-03 16:16
 **/
@Service
public class UpdateFileLogProcessor implements ItemProcessor<TlFilePaymentLog, TlFilePaymentLog> {

    @Resource
    private ExtTlThirdPartyPaymentMapper extTlThirdPartyPaymentMapper;
    @Resource
    private ExtAmLoanAccountInfoMapper extAmLoanAccountInfoMapper;
    @Resource
    private ExtAmRepaymentPlanInfoMapper extAmRepaymentPlanInfoMapper;
    @Resource
    private ExtTlPaymentAllocationLogMapper extTlPaymentAllocationLogMapper;
    @Resource
    private ExtAmLoanMainInfoMapper extLoanMainInfoMapper;


    @Override
    public TlFilePaymentLog process(TlFilePaymentLog item) throws Exception {
        AmLoanAccountInfo loan = extAmLoanAccountInfoMapper.selectByLoanIdForBatch(item.getLoanId());
        if(null == loan){
            return null;
        }
        TlThirdPartyPayment partyPayment = extTlThirdPartyPaymentMapper.selectByPayId(item.getTradeSeqNo(),loan.getCustomerId(),loan.getTenantId());
        if(null == partyPayment){
            return null;
        }

        TlFilePaymentLog updateLog = this.generateBankDeductionMessage(partyPayment);
        updateLog.setId(item.getId());
        return updateLog;
    }

    private TlFilePaymentLog generateBankDeductionMessage(TlThirdPartyPayment item) throws TxnBizException {

        TlFilePaymentLog updateLog = new TlFilePaymentLog();
        AmLoanMainInfo loanMainInfo = extLoanMainInfoMapper.selectByLoanIdAndCusId(item.getLoanId(), item.getCustomerId(), item.getTenantId());
        updateLog.setTotalAmt(item.getAmount());
        updateLog.setPrinAmt(item.getPrincipalAmount());
        updateLog.setIntrAmt(item.getInterestAmount());
        updateLog.setPentAmt(item.getPenaltyAmount().add(item.getCompoundAmount()));
        updateLog.setProductType(loanMainInfo.getLoanType());

        List<TlPaymentAllocationLog> logList = extTlPaymentAllocationLogMapper.selectBySeqNoAndTenantId(item.getSeqNo()
                , item.getTenantId());
        Map<Integer,List<TlPaymentAllocationLog>> tenorMap = logList.stream().collect(Collectors.groupingBy(TlPaymentAllocationLog::getBillingTenor));

        // 扣款期数
        if(TransBizConstant.PAYMENT_LOG_SUM_STATUS_S.equals(item.getSumStatus())) {
            if (TransBizConstant.REPAYMENT_TYPE_PAY.equals(item.getPaymentType())) {
                updateLog.setBillingTenor(logList.get(0).getBillingTenor());
            } else if (TransBizConstant.REPAYMENT_TYPE_PRETERMINTION.equals(item.getPaymentType())) {
                updateLog.setBillingTenor(0);
            } else if (TransBizConstant.REPAYMENT_TYPE_OVERDUE.equals(item.getPaymentType())) {
                if (tenorMap.size() > 1) {
                    updateLog.setBillingTenor(0);
                } else {
                    updateLog.setBillingTenor(logList.get(0).getBillingTenor());
                }
            }
            updateLog.setStatus(item.getSumStatus());
            updateLog.setReason("扣款成功");

        }else{
            updateLog.setStatus(TransBizConstant.LOAN_FAILED);
            updateLog.setBillingTenor(0);
            updateLog.setReason(item.getDescription());
        }
        return updateLog;
    }
}
